package Domain.UserClient;

import Domain.DataManager;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;
import Logger.ClientLogger;
import java.util.logging.Level;

public class ButtonsFunctions
{
        private static ButtonsFunctions _bf;
	private boolean _shouldClose;
	private StompClient _stompC;
        private DataManager _dm;

	private ButtonsFunctions(StompClient stompC, DataManager dm)
	{
		this._stompC = stompC;
		this._shouldClose = false;
                this._dm = dm;
	}

        public static ButtonsFunctions getButtonsFunctions(StompClient stompC, DataManager dm)
        {
                if(_bf == null)
                    _bf = new ButtonsFunctions(stompC, dm);
                return _bf;
        }

        public static ButtonsFunctions getButtonsFunctions()
        {
            if(_bf != null)
                return _bf;
            else
            {
                ClientLogger.write(Level.INFO,"Someone tried to call ButtonFunction before it has been initialized.");
                return null;
            }
        }
	
	public void processConnect(String name, String password)
	{
		this._stompC.sendStompFrame(StompFrame.createConnectFrame(name,password, ""));
                this._dm.setCurrUser(name);
	}

       	public void  processUnregister(String name)
	{
		this._stompC.sendStompFrame(StompFrame.createUnregisterFrame(name,"", ""));
	}



        public void processGetTypeOfUser()
        {
                this._stompC.sendStompFrame(StompFrame.createSendFrame("", "fserver", "get type of user"));
        }
	
	public void processRegisterUser(String name, String password)
	{
		this._stompC.sendStompFrame(StompFrame.createRegisterFrame(name,password, ""));
                this._dm.setCurrUser(name);
	}

        public void processAddUser()
        {
                this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","add user ;"));
        }
/*
	public void processRegisterManager(String name, String password)
	{
		this._stompC.sendStompFrame(StompFrame.createRegisterFrame(name,password, ""));
                this._stompC.sendStompFrame(StompFrame.createConnectFrame(name,password, ""));
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","add manager ;"));
                this._stompC.sendStompFrame(StompFrame.createSendFrame("", "fserver", "get type of user"));
                this._dm.setCurrUser(name);
	}
*/
	public void processDisconnect()
	{
		this._stompC.sendStompFrame(StompFrame.createDisconnectFrame(""));
        DataManager dMan = DataManager.getDataManager();
        dMan.setCurrForum("");
        dMan.setCurrThread("");
        dMan.setCurrUser("");
	}

	public void processExit()
	{
		this._stompC.sendStompFrame(StompFrame.createExitFrame(""));
		setShouldClose();
		System.exit(0);    //-----------------------can be changed if we use static database----------
	}

	public void processAddMessage(String thread, String title, String message)
	{
            String forum = this._dm.getCurrForum();
            if( forum != null & thread  != null )
            {
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","add message ; " 
                + forum + " ; " + thread + " ; "+ message + "\n" +  title));
            }
	}

        public void processEditMessage(String thread, String title, String newContent)
	{
            String forum = this._dm.getCurrForum();
            if( forum != null & thread  != null )
             //; [name of forum] ; [name of thread]; [name of message] ; [new content]
            {
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","edit message ; "
                + forum + " ; " + thread + " ; "+ title + ";" +  newContent));
            }
	}



	public void processAddThread(String thread)
	{
            String forum = this._dm.getCurrForum();
            if(forum != null)
            {
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","add thread ; " 
			      + forum + " ; " + thread));
            }
	}

	public void processAddForum(String forum)
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","add forum ; " + forum));
	}
	
	public void processGetMessages( String thread)
	{
                String forum = this._dm.getCurrForum();

                if(forum != null)
                {
                    this._stompC.sendStompFrame(StompFrame.createSendFrame("", "fserver", "get messages ; "
				+ forum + " ; " + thread));
                }
        }
	
	public void processGetThreads(String forum)
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","get threads ; " 
				+ forum));
                this._dm.setCurrForum(forum);
	}

        public void processRemoveMessage(String thread, String message)
        {
            String forum = this._dm.getCurrForum();
            if(forum != null)
            {//remove message  ; [name of forum] ; [name of thread ] ; [message]
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","remove message ; " +
                        forum + " ; " + thread + " ;" + message));
            }
        }
	
	public void processGetForums()
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","get forums ; "));
	}
	
	public void processGetUsers()
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","get users ; " ));
	}
	
	public void processAddFriend(String name)
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","add friend ; "
				+ name));
	}
	
	public void processRemoveFriend(String name)
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","remove friend ; "
				+ name));
	}

	public void processChangeToManager()
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","change type to manager ; " ));
	}
	
	public void processChangeToUser()
	{
		this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","change type to default ; " ));
	}

        public void processCheckAvailability(String name) {
            this._stompC.sendStompFrame(StompFrame.createAvailableFrame(name ));
        }

        public void processChangeUserToManager(String userName) {
            ClientLogger.write(Level.INFO,"changing type of other user to manager"+userName );
            this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","change type of other user to manager ; "+userName ));
        }

        public void processRemoveUser(String userName) {
             ClientLogger.write(Level.INFO,"removing user  "+userName);
            this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver","remove user  "+userName +";"));
        }

         public void processGetFriends() {
            this._stompC.sendStompFrame(StompFrame.createSendFrame("","fserver"," get friends of user "));
        }
        public String getCurrUser()
        {
            return this._dm.getCurrUser();
        }

	/**
	 * checks if to close the keyboardListener
	 * @return true if we need to close the keyboardListener
	 */
	public boolean getShouldClose()
	{
		return this._shouldClose;
	}

	/**
	 * sets close client to true
	 */
	public void setShouldClose(){
		this._shouldClose = true;
	}

    public String getCurrentForum() {
        return this._dm.getCurrForum();
    }

	
}
